<?php
declare (strict_types=1);

namespace app\api\controller;

use think\App;
use think\Validate;
use app\common\model\User;
use think\exception\ValidateException;

abstract class Base
{

    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;

    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;

    /**
     * 是否批量验证
     * @var bool
     */
    protected $batchValidate = false;

    /**
     * 系统参数
     * @var array
     */
    protected $system;

    /**
     * 管理员信息
     * @var object
     */
    protected $user;

    /**
     * 当前语言
     * @var object
     */
    protected $lang;

    /**
     * 过滤方法，择机使用
     * @var object
     */
    protected $filter = 'trim,strip_tags,addslashes,htmlspecialchars';

    public function __construct(App $app)
    {
        $this->app = $app;
        $this->request = $this->app->request;
        $this->user = $this->request->user;
        $this->system = $this->request->system;
        //全局过滤
        $this->request->filter($this->filter);
        // 控制器初始化
        $this->initialize();

    }

    public function initialize()
    {

    }

    protected function getPage($default = 1)
    {
        return intval($this->request->get('page', $default));
    }

    protected function getLimit($default = 20)
    {
        return intval($this->request->get('limit', $default));
    }

    /**
     * @param $token
     * @throws \Exception
     * 原点 2022年12月9日13:47:33 单独验证用户是否登录
     */
    protected function verifyToken($token = '')
    {
        try {
            $authorization = $this->request->header('Authorization', '');
            $token = $token ?: (preg_match('/^Bearer\s+(.*?)$/', $authorization, $matches) ? $matches[1] : '');
            if ($token) {
                $this->user = User::where('token', $token)->where('is_locking', 0)->find();
            }
            return true;
        } catch (\Exception $e) {
            return false;
        }

    }


    /**
     * 验证数据
     * @access protected
     * @param array $data 数据
     * @param string|array $validate 验证器名或者验证规则数组
     * @param array $message 提示信息
     * @param bool $batch 是否批量验证
     * @return array|string|true
     * @throws ValidateException
     */
    protected function validate(array $data, $validate, array $message = [], bool $batch = false)
    {
        if (is_array($validate)) {
            $v = new Validate();
            $v->rule($validate);
        } else {
            if (strpos($validate, '.')) {
                // 支持场景
                [$validate, $scene] = explode('.', $validate);
            }
            $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
            $v = new $class();
            if (!empty($scene)) {
                $v->scene($scene);
            }
        }

        $v->message($message);

        // 是否批量验证
        if ($batch || $this->batchValidate) {
            $v->batch(true);
        }

        return $v->failException(true)->check($data);
    }

}
